package javaapplication8.Make;

// $Source: /h/u1/szamosi/repo/b07/examples/parse/Rule.java,v $
// $Author: szamosi $
// $Date: 2007/06/24 00:25:12 $
// $Revision: 1.1.1.1 $

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

// Placeholder Makefile rule for use in parser examples.
class Rule {

    // Construct empty rule.
    public Rule() {
        init();
    }

    // Construct rule with target, pre-requisites, and actions.
    public Rule(String target, String[] prereqs, String[] actions) {
        init();
        fTarget = target;
        for (int i=0; i<prereqs.length; ++i) {
            fPrereqs.add(prereqs[i]);
        }
        for (int i=0; i<actions.length; ++i) {
            fActions.add(actions[i]);
        }
    }
  
    // Get rule's target.
    public String getTarget() {
        return fTarget;
    }

    // Set rule's target.
    public void setTarget(String target) {
        fTarget = target;
    }
  
    // Add a pre-requisite.
    public void addPrereq(String prereq) {
        fPrereqs.add(prereq);
    }
  
    // Iterate through pre-requisites.
    public Iterator getPrereqIter() {
        return fPrereqs.iterator();
    }
  
    // Add an action.
    public void addAction(String action) {
        fActions.add(action);
    }
  
    // Iterate through actions.
    public Iterator getActionIter() {
        return fActions.iterator();
    }
  
    // Convert to printable form.
    public String toString() {
        String result = fTarget + " :";
        Iterator ip = getPrereqIter();
        while (ip.hasNext()) {
            result += " " + (String)ip.next();
        }
        Iterator ia = getActionIter();
        while (ia.hasNext()) {
            result += "\n\t" + (String)ia.next();
        }
        return result;
    }

    //------------------------------------------------------------
  
    // Common initialization.
    protected void init() {
        fTarget  = null;
        fPrereqs = new ArrayList();
        fActions = new ArrayList();
    }

    //------------------------------------------------------------
  
    protected String    fTarget;        // target
    protected List      fPrereqs;       // pre-requisites
    protected List      fActions;       // actions
}
